require('./utils/index.js');
require('./config.js');
(function(G) {
  G.App({
    onError: function(error) {
      //G.reEnter('/pages/err/err?msg=系统错误,请退出微信重试!\r\n' + error);
    },
    onLogin: '/pages/login/login',
    autoLogin: function(success, fail) {
      try {
        var user = G.Storage.get('user', 1), groupId = G.Storage.get('groupId', 1);
        if (!user) {
          fail();
          return false;
        }
        user = G.string(user).parse();
        user.photo = typeof user.photo == 'string' && user.photo.charAt(0) === '/' ? G.Storage.get('APIURL') + user.photo : user.photo;
        G.Storage.set('userInfo', user);
        G.Storage.set('ajaxHeader', [{
            key: 'token',
            value: user.token
          }
        ], {
          type: 2
        })
        var tree = {
          '0': {
            children: [],
            active: false
          }
        }
        if (groupId){
          G.ajax('/api/model/group_menu', function (ret) {
            if (ret.errorMsg) {
              G.reEnter('/pages/err/err?msg=' + ret.errorMsg);
              return false;
            }
            var checkedId,location = G.location();
            for (var i = 0; i < ret.length; i++) {
              ret[i].parentId = ret[i].parentId || '0';
              ret[i].children = [];
              if ((ret[i].url == location) || (!checkedId && ret[i].url)) {
                checkedId = ret[i].id;
                if (tree[ret[i].parentId]) tree[ret[i].parentId].active = true;
                if (tree[tree[ret[i].parentId].parentId]) tree[tree[ret[i].parentId].parentId].active = true;
              }
              ret[i].active = false;
              tree[ret[i].id] = ret[i];
              if (tree[ret[i].parentId]) tree[ret[i].parentId].children.push(ret[i]);
            }
            G.setStore('masters',{ checkedId:checkedId, tree: tree['0'].children,user:user,groupId:groupId });
            success();
          }, {
              data: {
                groupId: groupId,
                unitId:'all',
                sql: 'app_group_menu'
              }
            })
        }else{
          G.setStore('masters',{ user:user });
          success();
        }
      } catch (e) {
        fail(e);
      }
    },
    Interceptor: function(data, next) {
      var _this = this;
      switch (data.code) {
        case 400:
          G.Storage.remove('user', 1);
          G.Storage.get('autoLogin')(function() {
            _this.action();
          })
          break;
        default:
          if (data.errMsg) data.errorMsg = data.errMsg;
          next(data);
          break;
      }
    }
  })
})(wx.$)